/*
 * Copyright (c) 2010. raoqn
 */

package com.yfeng.goodjuhome.service.impl;

import com.yfeng.common.dao.IDao;
import com.yfeng.common.dao.Param;
import com.yfeng.common.utils.DateUtil;
import com.yfeng.exception.ServiceException;
import com.yfeng.goodjuhome.model.MailModel;
import com.yfeng.goodjuhome.service.IHomeMailService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 站内邮件
 * www.goodjuhome.com
 * Created by IntelliJ IDEA.
 * User: rqn
 * Date: 2010-8-14
 * Time: 22:28:35
 */
@Service("homeMailService")
public class HomeMailServiceImpl implements IHomeMailService {

    private final Log log = LogFactory.getLog(this.getClass());

       @Autowired
       public IDao baseDao;

    
    public int sendMail(MailModel model) throws ServiceException {
        try
       {
           Param p = new Param();
           p.put("send_uid",model.getSend_uid());
           p.put("accept_uid",model.getAccept_uid());
           p.put("title",model.getTitle());
           p.put("content",model.getContent());
           p.put("createtime", DateUtil.getCurDateTime());
           int ret = baseDao.save("mails",p);
           return ret;
       }
       catch(Exception e)
       {
           log.error(e.getMessage(),e);
           throw new ServiceException(e.getMessage(),e);
       }
    }

    public int deleteMail(String[] ids) throws ServiceException {
       try {

			for (int i = 0; i < ids.length; i++) {
				baseDao.delete("mails", new Param("id", ids[i]));
			}
			return 1;
		} catch (Exception e) {
			log.error(e.getMessage(),e);
            throw new ServiceException(e.getMessage(),e);

		}
    }

    /**
     * 查询该用户的发送的站内邮件
     *
     * @param uid
     * @return
     * @throws com.yfeng.exception.ServiceException
     *
     */
    public List<Map> querySendedMail(String uid) throws ServiceException {
       try
        {
           List<Map> list = baseDao.query("select * from mails where send_uid=:uid",new Param("uid",uid));
           return list;
        }
        catch(Exception e)
        {
               log.error(e.getMessage(),e);
            throw new ServiceException(e.getMessage(),e);
        }
    }

    public List<Map> querySendedMail(String uid, String limit) throws ServiceException {
         try
        {
           List<Map> list = baseDao.query("select * from mails where send_uid=:uid limit "+limit,new Param("uid",uid));
           return list;
        }
        catch(Exception e)
        {
               log.error(e.getMessage(),e);
            throw new ServiceException(e.getMessage(),e);
        }
    }

    /**
     * 查询别人给发的邮件
     *
     * @param uid
     * @return
     * @throws com.yfeng.exception.ServiceException
     *
     */
    public List<Map> queryAcceptMail(String uid) throws ServiceException {
       try
        {
           List<Map> list = baseDao.query("select * from mails where accept_uid=:uid",new Param("uid",uid));
           return list;
        }
        catch(Exception e)
        {
               log.error(e.getMessage(),e);
            throw new ServiceException(e.getMessage(),e);
        }
    }

    public List<Map> queryAcceptMail(String uid, String limit) throws ServiceException {
       try
        {
           List<Map> list = baseDao.query("select * from mails where accept_uid=:uid limit "+limit,new Param("uid",uid));
           return list;
        }
        catch(Exception e)
        {
               log.error(e.getMessage(),e);
            throw new ServiceException(e.getMessage(),e);
        }
    }
}
